#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
import hashlib

from bs4 import BeautifulSoup

from . import app


def prettify(raw=None, expect=None):

    if (expect is None) or (raw is None):
        return "Bad Request!"

    # 自己的文章用不着做处理，直接返回
    pretty = raw
    # 从其他网站上怕的文章 ，带有html标签，如果有html标签则说明是爬的文章，就需要提取文本
    if re.match(r"^<+.{0,}", raw): 
        soup = BeautifulSoup(raw, "html.parser")
        pretty = "点进去看就知道啦！"
        if len(soup.select(expect))>3:
            pretty = soup.select(expect)[0].text + soup.select(expect)[1].text + soup.select(expect)[2].text
    return pretty

env = app.jinja_env
#注册自定义过滤器
env.filters['prettify'] = prettify




def calc_md5(msg):
    '''计算md5'''
    hash_md5 = hashlib.md5()
    if isinstance(msg, str):
        # 准备要计算md5的数据（bytes类型）
        msg = msg.encode('utf-8', errors='ignore')
    # 计算md5
    hash_md5.update(msg)
    # 计算结果(16进制字符串，32位字符)
    return hash_md5.hexdigest()





